import { isOn } from '@/shared'
import { createRenderer } from '../runtime-core'
function createElement(type) {
  return document.createElement(type)
}

function patchProp(el, key, preValue, nextValue) {
  if (isOn(key)) {
    el.addEventListener(key.slice(2).toLowerCase(), nextValue)
  } else {
    if (nextValue) {
      el.setAttribute(key, nextValue)
    } else {
      el.removeAttribute(key)
    }
  }
}

function remove(container) {
  const parent = container.parentNode
  if (parent) {
    parent.removeChild(container)
  }
}

function insert(el, container, anchor) {
  container.insertBefore(el, anchor)
}

function setElementText(el, text) {
  el.textContent = text
}

const renderer: any = createRenderer({
  createElement,
  patchProp,
  insert,
  remove,
  setElementText
})
export function createApp(...args) {
  return renderer.createApp(...args)
}
export * from '../runtime-core'
